var LoaderClass = Class.extend({
    counter: 0,
    visible: false,
    loader: 'dist_loader',
    init: function(loader){
        if (loader) {
            this.loader = loader;
        }
    },
    show: function(){
        this.counter++;
        if(!this.visible){
            this.display();
        }
    },
    hide: function(){
        this.counter--;  
        if(this.counter == 0){
            this.remove();
        }
    },
    display: function(){
        console.log("inserindo")
        $('#' + this.loader).removeClass('invisible');
        this.visible = true;
    },
    remove: function(){
        console.log("removendo")
        $('#' + this.loader).addClass('invisible');
        this.visible = false;
    }
});
var Loader = new LoaderClass();

var MessageClass = Class.extend({
    messageBox: 'message_box',
    messages: {},
    init: function(){
        
    },
    newMsg: function(type, message, id){
        var html = '<div class="' + type + ' message">' + message + '</div>';
        if(!id){
            id = this.messages.length + 1;
        } 
        this.messages[id] = html;
        this.display(html);
    },
    display: function(html){
        $('#' + this.messageBox).html(html);
        $('#' + this.messageBox).slideDown();
    },
    remove: function(id){
        if(id){
            this.messages[id] = null;
            $('#' + this.messageBox).html('');
            for(id in this.messages){
                this.display(this.messages[id]);
            }
        }else{
            this.clear();
        }       
    },
    clear: function(){
        $('#' + this.messageBox).html('');
    }
});

var Message = new MessageClass();

var AjaxRequest = Class.extend({
    options: {
        url:      '',
        data: '',
        dataType: 'json',
        type:     'POST',
        success:  function(){}
    },
    //retries: 0,
    init: function(options){
        this.options.dataType = 'json';
        this.options.type = 'POST';
        this.options = $.extend(this.options, options);
        this.options.url = base_url + this.options.url;
        this.request();
    },
    request: function(){
        var self = this;
        $.ajax({
          beforeSend: function(){
            Loader.show();
          },
          url: this.options.url,
          data: this.options.data,
          dataType: this.options.dataType,
          type: this.options.type,
          success: this.options.success,
          complete: function(){
            Loader.hide();
          },
          error: function( xhr, tStatus, err ) {
            Loader.hide();
            if( xhr.status === 401 || xhr.status === 403 || xhr.status === 500 || xhr.status === 404) {
                Message.newMsg('error', 'There was an error processing your request.<br />'
                                        + 'Please try again, or contact an administrator.<br />Code: '
                                        + xhr.status + ' Process: ' + self.url);
            } else if ( xhr.status === 504 && !retries++ ) {
                Message.newMsg('alert', 'The server was unresponsive to your request.<br />Resending...');
                self.request();
            }
          }
        });
    }
});